package com.xinzhi.service.impl;

import com.xinzhi.mapper.UserMapper;
import com.xinzhi.pojo.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utils.JwtUtil;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import com.xinzhi.utils.TengXunSMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TengXunSMSUtils tengXunSMSUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据手机号密码注册
     * @param phone
     * @param password
     * @param password2
     * @return
     */
    @Override
    public Result append(String phone, String password,String password2) {
        //先判断手机号是否为空
        if (phone==null || password==null ||password2==null)
            return Result.error("请输入手机号或者密码");
        //判断手机号是否正则
        if (!(isPhone(phone))|| !isPassword(password) ||!isPassword(password2))
            return Result.error("请输入正确的手机号或者密码");
        //判断用户输入的手机号是否在数据库中（这里的数据库的手机号也要进行查询)

        if (userMapper.selectPhone(phone)!=null)
            return Result.error("手机号已存在");

        if (!password.equals(password2))
            return Result.error("两次密码不一致");
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        //密码加密
        int i = userMapper.insertUser(phone, s);
        if (i>0){
            String userOrderNum = getUserOrderNum(phone);
            int i1 = userMapper.updateOrder(phone, userOrderNum);
            if (i1<=0){
                return Result.error("添加交易账户失败");
            }
        }
        String jwt = JwtUtil.createJWT(phone, s, 5L * 60L * 1000L);
        redisUtils.set(phone, jwt,5*60);

        return i>0 ? Result.ok("注册成功"):Result.error("注册失败");
       }

    /**
     * 判断手机号是否正则
     * @param phone
     * @return
     */
   @Override
   public boolean isPhone(String phone) {
       boolean matches = phone.matches("^1[3456789]\\d{9}$");
       return matches;
    }

    /**
     * 判断密码是否正则
     * @param password
     * @return
     */
    @Override
    public boolean isPassword(String password) {
        boolean matches = password.matches("^[a-zA-Z0-9]{8,30}$");
        return matches;
   }

    /**
     * 根据手机号修改密码
     * @param phone   手机号
     * @param password  密码
     * @param password2  确认密码
     * @return
     */

    @Override
    public Result update(String phone, String password, String password2) {
        System.out.println(phone+" "+password+" "+password2);
        if (phone == null )
            return  Result.error("手机号不能为空");
        if(!isPhone(phone)){
            return Result.error("请输入正确的手机号");
        }
        if(password==null || password2==null){
            return Result.error("请输入密码");
        }
        if (!isPassword(password)||!isPassword(password2)){
            return Result.error("请输入正确密码");
        }
        if (!password.equals(password2))
            return Result.error("两次密码不一致");
        //加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        //去redis删除缓存（原有的密码）
        redisTemplate.delete("password");
        //数据库执行修改操作
        int i = userMapper.updateUser(phone, s);
        //睡眠
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //删除缓存（修改后的密码）
        redisTemplate.delete("password");
        String o = (String) redisUtils.get("password");
        System.out.println(o);
        return i>0 ? Result.ok("修改成功"):Result.error("修改失败");


    }

    /**
     * 根据手机号查询用户id
     * @param phone
     * @return
     */
    @Override
    public Result selectUser(String phone) {
        User selectid = userMapper.selectid(phone);
        Integer id = selectid.getId();
        return Result.ok(id);
    }
    /**
     * 根据手机号查询用户信息
     * @param phone
     * @return
     */
    @Override
    public Result selectUsers(String phone) {
        User user = userMapper.selectUser(phone);
        return user!=null?Result.ok(user):Result.error("400");
    }


    /**
     * 获取交易账户
     * @return
     */
    @Override
    public String getUserOrderNum(String phone) {
        User user = userMapper.selectPhone(phone);
        String orderNumber = "05103323"+user.getId();
        return orderNumber;
    }

    /**
     * 修改头像
     * @param headPhoto
     * @return
     */
    @Override
    public Result updateHead(String headPhoto , String phone) {
        if (headPhoto==null)
            return Result.error("请上传头像");
        int i = userMapper.updateHead(headPhoto,phone);
        return i>0?Result.ok("上传成功"):Result.error("上传失败");
    }


}

